var currentTableID = 0;
//var tableTims = {'1':{status:true,time:558},'2':{status:false,time:58},'3':{status:false,time:0},'4':{status:false,time:0}};
var waitArr = [];
var bodyFree  = '<button class="btn btn-large btn-danger"  onclick="tableBusy();">Busy</button> <button class="btn btn-large btn-warning"  onclick="tabelWait();">Reserv</button>'; 
var bodyBusy  = '<button class="btn btn-large btn-success"  onclick="tableFree();">Free</button> <button class="btn btn-large btn-warning"  onclick="tabelWait();">Busy</button>'; 
var bodyWait  = '<button class="btn btn-large btn-success"  onclick="tableFree();">Free</button> <button class="btn btn-large btn-danger"  onclick="tableBusy();">Busy</button>'; 

$(document).ready(function() {
	setPopover();
	changeTimes();
	self.setInterval(function(){changeTimes();},1000);
	//reservShoweStart(1);
	//reservShoweStart(3);
	});
function selectTable(event){
	if (currentTableID && currentTableID != event.currentTarget.id) {
		$("#"+currentTableID).popover("hide");
		currentTableID = event.currentTarget.id;
	} else {
		currentTableID = event.currentTarget.id;
	}
	reservShoweStop(currentTableID.toString());
} 

function tableFree() {
	$("#box_"+currentTableID).removeClass("box-busy");
	$("#"+currentTableID).removeClass("table-busy");
	$("#box_"+currentTableID).addClass("box-free");
	$("#"+currentTableID).addClass("table-free");
	$("#"+currentTableID).popover("hide");
	$("#"+currentTableID).popover('destroy');
	$("#"+currentTableID).popover({title:"barev",html:true,content:bodyFree, placement: 'top'});
	tableTims[""+currentTableID].status = false;
	$("#time_"+currentTableID).html("");
	$.remoteCall('ADMIN_tables','tableStatusChange',[currentTableID,0],callBack);
}
function tableBusy() {
	$("#box_"+currentTableID).removeClass("box-free");
	$("#"+currentTableID).removeClass("table-free");
	$("#box_"+currentTableID).addClass("box-busy");
	$("#"+currentTableID).addClass("table-busy");
	$("#"+currentTableID).popover("hide");
	$("#"+currentTableID).popover('destroy');
	$("#"+currentTableID).popover({title:"barev",html:true,content:bodyBusy, placement: 'top'});
	tableTims[""+currentTableID].status = true;
	tableTims[""+currentTableID].time = 0;
	$("#time_"+currentTableID).html("00 : 00");
	$.remoteCall('ADMIN_tables','tableStatusChange',[currentTableID,1],callBack);
}
function setPopover() {
	$("span.table-free").popover({title:"barev",html:true,content:bodyFree, placement: 'top'});
	$("span.table-busy").popover({title:"barev",html:true,content:bodyBusy, placement: 'top'});
}
function tabelWait() {
	var ff = 55;
}

function changeTimes() {
	for( var key in tableTims ) {
		var val = tableTims[key];
		if( val.status == true ) {
			val.time++;
			drawTime(key,val.time);
		}
	}
}
function drawTime(id,time) {
	time = parseInt(time/60);
	var H = "00";
	var M = "00";
	var h = parseInt((time/60)%24);
	var m = parseInt(time%60);
	if( h<10 ) {
		H = "0"+h;
	} else {
		H = ""+h;
	}
	if( m<10 ) {
		M = "0"+m;
	} else {
		M = ""+m;
	}
	$("#time_"+id).html(H+" : "+M);
}
function reservShoweStart(id){
	waitArr.push(""+id);
	//$("#box_"+id).removeClass("box-free");
	//$("#box_"+id).removeClass("box-busy");
	$("#box_"+id).addClass("box-wait");
	waitFewToVery(id.toString());
}
function reservShoweStop(id){
	$("#box_"+id).removeClass("box-wait");
	$("#box_"+id).removeAttr("style");
	for( var i=0;i<waitArr.length;i++ ){
		if( waitArr[i] == id ){
			waitArr.splice(i,1);
			break;
		}
	}
}
function waitFewToVery(id){
	var ok = false;
	for( var i=0;i<waitArr.length;i++ ){
		if( waitArr[i] == id ){
			ok = true;
			break;
		}
	}
	if( ok ) {
		$("#box_"+id).animate({boxShadow:"inset 0 0 85px orange"},{
			duration: 700,
			complete: function() { waitVeryToFew(id); }
		});
	}
}
function waitVeryToFew(id){
	var ok = false;
	for( var i=0;i<waitArr.length;i++ ){
		if( waitArr[i] == id ){
			ok = true;
			break;
		}
	}
	if( ok ) {
		$("#box_"+id).animate({boxShadow:"inset 0 0 5px orange"},{
			duration: 700,
			complete: function() { waitFewToVery(id); }
		});
	}
}
function callBack(){
	var result = this.result;
}